var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var app = getApp();

Page({
  data: {
    bannerInfo: {
      'img_url': '',
      'name': ''
    },
    categoryFilter: false,
    filterCategory: [{ id: 1, name: '星级', checked: false }, { id: 2, name: '青旅', checked: true }],
    categoryId: 0,
    currentSortType: 'default',
    currentSortOrder: 'desc',
    pageNum: 1, 
    pageNum_other:0,
    level:'',
    pageSize: 6,
    city: "",
    hotels: [],
    hotels_other:[],
    moMore: false,
    type:"",
    noMore_other:false,
    winHeight:0
  },
  getData: function () {
    let that = this;
    var data = {
      pageSize: that.data.pageSize,
      pageNum: that.data.pageNum,
      regionName: that.data.city,
      startDate: that.data.startDay,
      endDate: that.data.endDay,
      keyWord: that.data.keyWord, level: that.data.level,
      orderType: that.data.currentSortType,
      isDesc: that.data.currentSortOrder,
      category: that.data.categoryId
    }
    util.request(api.HotelList, data).then(function (res) {
      if (res.code == 200) {
        if (!res.datas || res.datas.length <= 0) {
          that.setData({
            noMore: true
          })
        }
        that.setData({
          hotels: res.datas
        });
      }
    });
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    var city = options.city;
    that.setData({
      city: city,
      startDay: options.startDay,
      endDay: options.endDay,
      keyWord: options.keyWord,
      level: options.level,
      type: wx.getStorageSync("type")
    });
    that.getData();
    wx.setNavigationBarTitle({
      title: city,
    })
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winHeight:res.windowHeight
        })
      },
    })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'categoryFilter':
        this.setData({
          'categoryFilter': !this.data.categoryFilter,
          'currentSortType': 'category',
          'currentSortOrder': 'asc'
        });
        break;
      case 'priceSort':
        let tmpSortOrder = 'asc';
        if (this.data.currentSortOrder == 'asc') {
          tmpSortOrder = 'desc';
        }
        this.setData({
          'currentSortType': 'price',
          'currentSortOrder': tmpSortOrder,
          'categoryFilter': false,
          pageNum:1
        });

        this.getData();
        break;
      default:
        //综合排序
        this.setData({
          'currentSortType': 'default',
          'currentSortOrder': 'desc',
          'categoryFilter': false
        });
        this.getData();
    }
  },
  selectCategory: function (event) {
    let currentIndex = event.target.dataset.categoryIndex;
    var that = this;
    var filterCategory = that.data.filterCategory;
    for (var i = 0; i < filterCategory.length; i++) {
      if (i == currentIndex) {
        filterCategory[i].checked = true;
      } else {
        filterCategory[i].checked = false;
      }
    }
    this.setData({
      'categoryFilter': false,
      'categoryId': this.data.filterCategory[currentIndex].id,
      filterCategory: filterCategory
    });
    this.getData();

  },
  fnGetMore: function () {
    var that = this;
    if(that.data.noMore){
      that.fnGetMoreData_other();
    }else{
      that.fnGetMoreData();
    }
  },
  inputChange: function (e) {

    this.setData({
      keyWord: e.detail.value,
      searchStatus: false
    });
    // this.getHelpKeyword();
  },
  fnGetMoreData:function(){
    var that = this;
    var data = {
      pageSize: that.data.pageSize,
      pageNum: that.data.pageNum + 1,
      regionName: that.data.city,
      startDate: that.data.startDay,
      endDate: that.data.endDay,
      keyWord: that.data.keyWord,
      orderType: that.data.currentSortType,
      isDesc: that.data.currentSortOrder,
      category: that.data.categoryId
    }
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.HotelList, data).then(function (res) {
      wx.hideLoading();
      if (res.code == 200) {
        if (res.datas && res.datas.length == that.data.pageSize) {
          //数据够显示一页
          that.setData({
            hotels: that.data.hotels.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: false
          })
        } else if (res.datas && res.datas.length > 0) {
          //数据不够够显示一页
          that.setData({
            hotels: that.data.hotels.concat(res.datas),
            pageNum: that.data.pageNum + 1,
            noMore: true
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            image: "/static/images/icon_error.png",
            duration: 2000
          })
          that.setData({
            noMore: true
          })
        }
      }
    });
  },
  fnGetMoreData_other: function () {
    var that = this;
    var data = {
      pageSize: that.data.pageSize,
      pageNum: that.data.pageNum_other + 1,
      regionName: that.data.city,
      startDate: that.data.startDay,
      endDate: that.data.endDay,
      keyWord: that.data.keyWord,
      orderType: that.data.currentSortType,
      isDesc: that.data.currentSortOrder,
      category: that.data.categoryId,
      isOtherCity:1
    }
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.HotelList, data).then(function (res) {
      wx.hideLoading();
      if (res.code == 200) {
        if (res.datas && res.datas.length == that.data.pageSize) {
          //数据够显示一页
          that.setData({
            hotels_other: that.data.hotels_other.concat(res.datas),
            pageNum_other: that.data.pageNum_other + 1,
            noMore_other: false
          })
        } else if (res.datas && res.datas.length > 0) {
          //数据不够够显示一页
          that.setData({
            hotels_other: that.data.hotels_other.concat(res.datas),
            pageNum_other: that.data.pageNum_other + 1,
            noMore_other: true
          })
        } else {
          wx.showToast({
            title: '没有更多了',
            image: "/static/images/icon_error.png",
            duration: 2000
          })
          that.setData({
            noMore_other: true
          })
        }
      }
    });
  },
  search:function(e){
    var that = this
    that.setData({
      pageNum: 1,
      pageSize: 10, 
      keyWord: that.data.keyWord,
       level: that.data.level,
    })
   that.getData();
  },
  onKeywordConfirm(event) {
    var that = this
    that.setData({
      pageNum: 1,
      pageSize: 10,
    })
    that.getData();
  },
  fnShare: function (e) {
    var that = this;
    var path = "";
    var type = e.currentTarget.dataset.type;
    var goodsId = e.currentTarget.dataset.id;
    var imgUrl = e.currentTarget.dataset.url;
    var goodsName = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    if (type == 'hotel') {
      path = "/pages/hotelDetail/hoteldetail";
    } else if (type == "scene") {
      path = "/pages/sceneDetail/sceninfo";
    } else {
      path = "/pages/tourDetail/travelline"
    }
    app.fnShareGoods(path, goodsId, imgUrl, goodsName, "hotel", address, price, marketPrice);
  },selectCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  }
})